
#include "PreHeader.h"
#include "cEnemy.h"

#include "cLogicDataManager.h"

cEnemy::cEnemy()
{
	m_enemyType = eEnemyType::NORMAL;
	m_foodID = 0;
	m_speed = 0.0f;

	m_doubleHP = false;
	m_zigzagMove = false;
	m_kernelBreaked = false;
}

cEnemy::~cEnemy()
{

}

void cEnemy::Init(const sEnemyDesc& desc)
{
	cEntity::Init(desc.entityDesc);

	m_sprite->setScale(1.5f);

	m_enemyType = desc.enemyType;
	m_foodID = desc.foodID;
	m_speed = desc.speed;
	
	// move start
	SetPosv(ccp(0.0f, -m_speed));
}

void cEnemy::Update(float dt)
{
	cEntity::Update(dt);

	const b2Vec2& bodyPos = m_body->GetPosition();
	CCPoint pos = ccp(bodyPos.x*PTM_RATIO, bodyPos.y*PTM_RATIO);
	if (pos.y < 0.0f)
	{
		m_deleted = true;
	}
}

void cEnemy::OnHit()
{
	if (m_enemyType == eEnemyType::BREAKABLE)
	{
		if (m_kernelBreaked == true)
		{
			m_deleted = true;
		}
		else
		{
			const nFoodFormat01::sFood* foodData = cLogicDataManager::it()->GetFoodData(m_foodID);

			m_enemyType = eEnemyType::NORMAL;
			m_sprite->initWithFile(foodData->kernelSpriteFilename.c_str());

			b2Fixture* fixture = m_body->GetFixtureList();
			while (fixture != NULL)
			{
				b2Filter filter = fixture->GetFilterData();
				filter.categoryBits = 0;
				filter.maskBits = 0;
				filter.groupIndex = -1;
				fixture->SetFilterData(filter);

				fixture = fixture->GetNext();
			}

			m_kernelBreaked = true;
		}
	}
	else
	{
		m_deleted = true;
	}
}
